package interpreter

import (
	"kumachan/standalone/util/fatal"
	"kumachan/standalone/util/richtext"
	"kumachan/interpreter/compiler"
	"kumachan/interpreter/program"
	"kumachan/interpreter/runtime"
)


func Compile(root string) (*program.Program, *compiler.NsHeaderMap, *compiler.Executable, fatal.Exception) {
	if root == "" {
		return CreateBlankProgram()
	}
	var project, err = compiler.LoadProject(root)
	if err != nil { return nil, nil, nil, richtext.MakeStdError(err) }
	var fs = compiler.RealFileSystem {}
	var meta = program.Metadata { ProgramPath: root }
	var p, ctx, exe, errs1, errs2 = compiler.Compile(project, fs, meta)
	if errs1 != nil { return nil, nil, nil, errs1 }
	if errs2 != nil { return nil, nil, nil, errs2 }
	return p, ctx, exe, nil
}
func Run(p *program.Program, env *runtime.Environment) {
	var exit = make(runtime.ExitSignal)
	runtime.Run(p, "", env, exit)
	<- exit
}

func OrdinaryEnvironment(args ([] string)) *runtime.Environment {
	return &runtime.Environment {
		Args: args,
		// TODO
	}
}


